package com.cms.manage.controller;

import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;


/**
 * 控制器常用的�?些方�?
 * @author Administrator
 */
public class BaseController <T>{
	/**
	 * 返回错误的platformType
	 * @param message
	 * @return
	 */
	protected Map<String,Object> WRONG(T message){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("status", 500);
		map.put("message", message);
		return map;
	}

	/**
	 * 返回失效的token
	 * @param message
	 * @return
	 */
	protected Map<String,Object> INVALID(T message){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("status", 400);
		map.put("message", message);
		return map;
	}
	/**
	 * token错误
	 * @param message
	 * @return
	 */
	protected Map<String,Object> ERRORTOKEN(T message){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("status", 401);
		map.put("message", message);
		return map;
	}

	/**
	 * 权限不足
	 * @param message
	 * @return
	 */
	protected Map<String,Object> ERRORFORBIDDEN(T message){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("status", 403);
		map.put("message", message);
		return map;
	}
	/**
	 * token错误时返回的信息
	 * @param message
	 * @return
	 */
	protected Map<String,Object> ERROR(T message){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("status", 0);
		map.put("message", message);
		return map;
	}
	protected Map<String,Object> ERROR(T message,T list){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("status", 0);
		map.put("message", message);
		map.put("data", list);
		return map;
	}
	 /**
     * 登录失效
     *
     * @param message
     * @return
     */
    protected Map<String, Object> NO_LOGIN() {
        Map<String, Object> map = new HashMap<>();
        map.put("status", 3);
        map.put("message", "未登录");
        return map;
    }
    /**
     * 登录失效
     *
     * @param message
     * @return
     */
    protected Map<String, Object> LONIN_TIMEOUT() {
        Map<String, Object> map = new HashMap<>();
        map.put("status", 2);
        map.put("message", "登录已过期");
        return map;
    }
	/**
	 * 返回成功
	 * @return
	 */
	protected Map<String,Object> SUCCESS(){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("status", 1);
		map.put("message", "成功");
		return map;
	}
	
	protected Map<String,Object> SUCCESS(T message){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("status", 1);
		map.put("message", message);
		return map;
	}
	
	protected Map<String,Object> SUCCESS(T message,T list){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("status", 1);
		map.put("message", message);
		map.put("data", list);
		return map;
	}

	/**
	 * 返回成功
	 * @return
	 */
	protected Map<String,Object> SUCCESS(int page, int number, int totalSize, int totalPage, List<?> list){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("status",1);
		map.put("message", "成功");
		map.put("page", page);
		map.put("number", number);
		map.put("totalSize", totalSize);
		map.put("totalPage", totalPage);
		map.put("data", list);
		return map;
	}
	
	/**
	 * 返回成功
	 * @return
	 */
	protected Map<String,Object> SUCCESS(int page, int number, int totalSize, int totalPage, List<?> list,List<?> dataOther){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("status", 1);
		map.put("message", "成功");
		map.put("page", page);
		map.put("number", number);
		map.put("totalSize", totalSize);
		map.put("totalPage", totalPage);
		map.put("data", list);
		map.put("dataOther", dataOther);
		return map;
	}
	
	/**
	 * 返回成功
	 * @return
	 */
	protected Map<String,Object> SUCCESS(int page, int number, int totalSize, int totalPage, Map<String, Object> dataMap){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("status", 1);
		map.put("message", "成功");
		map.put("page", page);
		map.put("number", number);
		map.put("totalSize", totalSize);
		map.put("totalPage", totalPage);
		map.putAll(dataMap);
		return map;
	}
	
	/**
	 * 返回成功
	 * @return
	 */
	protected Map<String,Object> SUCCESS(List<?> list){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("status", 1);
		map.put("message", "成功");
		map.put("data", list);
		return map;
	}
	
	/**
	 * 返回成功
	 * @return
	 */
	protected Map<String,Object> SUCCESS(Map<String,Object> map){
		map.put("status", 1);
		return map;
	}
	
	protected Map<String,Object> ERROR(Map<String,Object> map){
		map.put("status", 0);
		return map;
	}
	
	/**
	 * 日期转换
	 * @param binder 
	 * @return
	 */
	@InitBinder
    public void init(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new MyDateEditor());
    }
    
     private class MyDateEditor extends PropertyEditorSupport {
            @Override
            public void setAsText(String text) throws IllegalArgumentException {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = null;
                try {
                    date = format.parse(text);
                } catch (ParseException e) {
                    format = new SimpleDateFormat("yyyy-MM-dd");
                    try {
                        date = format.parse(text);
                    } catch (ParseException e1) {
                    }
                }
                setValue(date);
            }
     }
}
